﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class RuleExecution : System.Web.UI.Page
{
    #region display rule details
    protected void LBRule_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!populateRuleDetails()) return;
        if (!populateSubRulesList()) return;
        if (!populateCycleDetails()) return;
    }

    private Boolean populateCycleDetails()
    {
        String query = "SELECT CycleId, (CONVERT(varchar,StartDate,111) + ' - ' +CONVERT(varchar,EndDate,111)) AS CycleDates ";
        query += "FROM CycleStatus WHERE Status NOT IN ('Billed') AND RuleId = '" + LBRuleList.SelectedValue + "'";

        DataTable cycles = DataBaseMethods.ExecuteSelectQuery(query);

        if (cycles.Rows.Count <= 0)
        {
            Logger.Show(Logger.RULES_NOT_EXIST + "Table : CycleStatus");
            return false;
        }
        else
        {
            LBCycleList.DataSource = cycles;
            LBCycleList.DataBind();
        }

        return true;
    }

    private Boolean populateSubRulesList()
    {
        String query = "SELECT f.RowNo, f.Name FROM Formula f, RuleRelations r where r.SubRuleId = f.RowNo ";
        query += " AND r.RuleId = '" + LBRuleList.SelectedValue + "' ORDER BY r.Priority";

        DataTable subRules = DataBaseMethods.ExecuteSelectQuery(query);

        if (subRules.Rows.Count <= 0)
        {
            Logger.Show(Logger.RULES_NOT_EXIST + "Table : RuleRelations");
            return false;
        }
        else
        {
            LBSubRuleList.DataSource = subRules;
            LBSubRuleList.DataBind();
        }

        return true;
    }

    private Boolean populateRuleDetails()
    {
        //retrieve details
        string select = "SELECT [Name],[FromDate],[ToDate],[Repeats],[DMY],[DMYCount] FROM [RuleDetails] WHERE [RowNo] = '" + LBRuleList.SelectedValue + "'";
        DataTable dt = DataBaseMethods.ExecuteSelectQuery(select);

        //there should be only one entry
        if (dt.Rows.Count != 1)
        {
            Logger.Show(Logger.RULES_NOT_EXIST + "Table : RuleDetails");
            return false;
        }
        DataRow entry = dt.Rows[0];

        //populate details on screen
        try
        {
            TBRuleName.Text = entry["Name"].ToString();
            TBFromDate.Text = entry["FromDate"].ToString();
            TBToDate.Text = entry["ToDate"].ToString();
            if (entry["Repeats"].ToString() == "True")
                TBFrequency.Text = "Every : " + entry["DMYCount"].ToString() + " " + entry["DMY"].ToString();
            else
                TBFrequency.Text = "Never";
        }
        catch (Exception ex)
        {
            Logger.Show(Logger.ERROR_WHILE_DISPLAY + "Message :" + ex.Message + ".");
            return false;
        }
        return true;
    }
    #endregion

    #region execute rule
    protected void BExecute_Click(object sender, EventArgs e)
    {
        if (LBCycleList.SelectedIndex < 0)
        {
            Logger.Show("Please select CYCLE for billing.");
            return;
        }
        int rule = Convert.ToInt32(LBRuleList.SelectedValue);
        String cycle = LBCycleList.SelectedItem.Text;

        ExecuteRule er = new ExecuteRule(); 
        er.BillSelectedRuleCycle(rule, cycle);
    }
    #endregion

    #region unselectable subrule
    protected void LBSubRuleList_SelectedIndexChanged(object sender, EventArgs e)
    {
        LBSubRuleList.SelectedIndex = -1;
    }
    #endregion
}
